package com.zjitc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjitc.model.Cart;
import com.zjitc.model.Order;
import com.zjitc.model.OrderItem;
import com.zjitc.model.User;
import com.zjitc.service.CartService;
import com.zjitc.service.OrderService;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/24 14:36
 * @description:
 */
@Controller
@RequestMapping("/order")
public class OrderController {

  @Autowired
  private OrderService orderService;

  @Autowired
  private CartService cartService;

  /**
   * 生成订单
   * @param session
   * @param total
   * @return
   */
  @RequestMapping(value = "/createOrder")
  private String createOrder(HttpSession session, Double total){
    User user = (User) session.getAttribute("user");
    Order order = new Order();

    //生成一个订单对象order(id uuid 时间 未付款 收货人信息 用户)
    order.setOid(String.valueOf(UUID.randomUUID()));
    order.getOrdertime(LocalDateTime.now());
    order.setState(0);
    order.setUid(user.getUid());
    order.setUser(user);

    //根据遍历cartList购物车条目信息，生成订单条目
    List<Cart> cartList = cartService.findCartByUid(user.getUid());
    List<OrderItem> orderItems = new ArrayList<>();
    for (Cart item : cartList) {
      OrderItem orderItem = new OrderItem();
      orderItem.setOsatate(0);
      orderItem.setCount(item.getQuantity());
      orderItem.setSubtotal((double) (item.getQuantity() * item.getProduct().getPrice()));
      orderItem.setOid(order.getOid());
      orderItem.setPid(item.getProduct().getPid());
      orderService.createOrderItem(orderItem);
      orderItems.add(orderItem);

      //订单条目是个集合，要把它放在订单下面
      order.setOrderItemList(orderItems);
      order.setTotal(total);

      //把order订单插入数据库
      orderService.createOrder(order);
      cartService.cleanCart(user.getUid());

      //order放到request域中，转到订单页面
      session.setAttribute("order", order);
    }
    return "shopping_handle";
  }

  /**
   * 查询订单
   * @param start
   * @param count
   * @param session
   * @param uid
   * @return
   */
  @RequestMapping(value = "/findOrder")
  private String findOrder(
      @RequestParam(name = "start", required = false, defaultValue = "1") int start,
      @RequestParam(name = "count", required = false, defaultValue = "10") int count,
      HttpSession session ,
      Integer uid
  ){
    List<Order> list = orderService.findOrder(uid);
    PageHelper.startPage(start,count);
    PageInfo<Order> pageInfo = new PageInfo<>(list);
    session.setAttribute("list",list);
    session.setAttribute("uid",uid);
    session.setAttribute("page",pageInfo);
    return "orderList";
  }

  /**
   * 删除订单
   * @param session
   * @param oid
   * @return
   */
  @RequestMapping(value = "/deleteOrder")
  private String deleteOrder(HttpSession session, String oid){
    orderService.deleteOrder(oid);
    Integer uid = (Integer) session.getAttribute("uid");
    //重定向到findOrder.do
    return "redirect:/order/findOrder.do?uid="+uid;
  }

  /**
   * 订单结算
   * @param session
   * @param name
   * @param phone
   * @param address
   * @return
   */
  @RequestMapping(value = "/orderPayment")
  private String orderPayment(HttpSession session, String name, String phone, String address){
    Order order = (Order) session.getAttribute("order");
    orderService.orderPayment(order.getOid(),name,phone,address);
    String total = String.valueOf(order.getTotal());
    //订单号
    session.setAttribute("Order Number",order.getOid());
    //订单总金额
    session.setAttribute("Amount of payment", total);
    //订单名称
    session.setAttribute("Order Name","name");
    //商品描述
    session.setAttribute("Commodity Description","product");
    return "alipay.trade.page.pay";
  }

  /**
   * 支付成功，重新跳回主页面
   * @return
   */
  @RequestMapping(value = "/paySuccess")
  private String paySuccess(HttpSession session) {
    Order order = (Order) session.getAttribute("order");
    orderService.updateState(order.getOid());
    return "main";
  }
}
